package com.df.tv.launcher.view;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.os.Handler;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.df.tv.launcher.R;
import com.df.tv.launcher.activity.DetailActivity;
import com.telecom.tv189.comlib.dynamicui.DynamicView;
import com.telecom.tv189.comlib.dynamicui.json.ActionPerformer;
import com.telecom.tv189.comlib.dynamicui.json.ActionPerformer.PerformException;
import com.telecom.tv189.comlib.models.ClickLogSender;
import com.telecom.tv189.comlib.util.Attributes;
import com.telecom.tv189.comlib.view.XUtilsImageView;

public class Preview extends RelativeLayout implements DynamicView {

    // xxhuang add
    // for auto gen
    public static final String CLASS_PATTERN = "Preview";

    public static final String ATTR_PREVIEW_STYLE = "previewStyle";
    public static final String ATTR_MOVIE_TITLE = "movieTitle";
    // public static final String ATTR_MOVIE_YEAR = "movieYear";
    // public static final String ATTR_MOVIE_DATE = "movieDate";
    // public static final String ATTR_MOVIE_RATING = "movieRating";
    public static final String ATTR_IMAGE_SRC = "imageSrc";
    public static final String ATTR_IMAGE_TAG = "tag"; // not real tag of
                                                       // view,just attr;
    // xxhuang end

    // preview style constant. add by xxhuang
    public static final int PREVIEW_STYLE_NONE = -1;
    public static final int PREVIEW_STYLE_TITLE = 0;
    public static final int PREVIEW_STYLE_RATING = 1;
    public static final int PREVIEW_STYLE_DATE = 2;

    private XUtilsImageView mMovieImg;
    private TextView mMovieTitle;
    private TextView mMovieYear;
    private TextView mMovieDate;
    private RatingView mRating;
    private View mButtomBar = null;

    public Preview(Context context) {
        super(context);
        init(context, 0);
    }

    public Preview(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray styles = context.obtainStyledAttributes(attrs, R.styleable.preview);
        int style = styles.getInt(R.styleable.preview_style, -1);
        init(context, style);
    }

    private void init(Context context, int style) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View mPreview = null;
        switch (style) {
        case PREVIEW_STYLE_NONE:
            mPreview = inflater.inflate(R.layout.preview_none, this);
            mMovieImg = (XUtilsImageView) mPreview.findViewById(R.id.iv_movie_img);
            break;
        case PREVIEW_STYLE_TITLE:
            mPreview = inflater.inflate(R.layout.preview_title, this);
            mMovieImg = (XUtilsImageView) mPreview.findViewById(R.id.iv_movie_img);
            mMovieTitle = (TextView) mPreview.findViewById(R.id.tv_movie_title);
            mMovieYear = (TextView) mPreview.findViewById(R.id.tv_movie_year);
            mButtomBar = mPreview.findViewById(R.id.buttomBar_view);

            break;
        case PREVIEW_STYLE_RATING:
            mPreview = inflater.inflate(R.layout.preview_rating, this);
            mMovieImg = (XUtilsImageView) mPreview.findViewById(R.id.iv_movie_img);
            mMovieTitle = (TextView) mPreview.findViewById(R.id.tv_movie_title);
            mMovieYear = (TextView) mPreview.findViewById(R.id.tv_movie_year);
            mRating = (RatingView) mPreview.findViewById(R.id.rv_rating);
            mButtomBar = mPreview.findViewById(R.id.buttomBar_view);
            break;
        case PREVIEW_STYLE_DATE:
            mPreview = inflater.inflate(R.layout.preview_date, this);
            mMovieImg = (XUtilsImageView) mPreview.findViewById(R.id.iv_movie_img);
            mMovieTitle = (TextView) mPreview.findViewById(R.id.tv_movie_title);
            mMovieDate = (TextView) mPreview.findViewById(R.id.tv_movie_date);
            mButtomBar = mPreview.findViewById(R.id.buttomBar_view);
            break;

        default:
            break;
        }
    }

    public void setPreviewImgByURL(String url) {
        mMovieImg.setImage(url);
    }

    public void setPreviewImg(Bitmap bm) {
        if (mMovieImg != null && bm != null) {
            mMovieImg.setImageBitmap(bm);
        }
    }

    public void setPreviewImg(int res) {
        if (mMovieImg != null) {
            mMovieImg.setImageResource(res);
        }
    }

    public void setPreviewTitle(String title) {
        if (mMovieTitle != null && !TextUtils.isEmpty(title)) {
            if (mButtomBar != null && mButtomBar.getVisibility() != View.VISIBLE) {
                mButtomBar.setVisibility(View.VISIBLE);
            }
            mMovieTitle.setText(title);
        }
        else {
            if (mButtomBar != null && mButtomBar.getVisibility() != View.GONE) {
                mButtomBar.setVisibility(View.GONE);
            }
        }
    }

    public void setPreviewYear(String year) {
        if (mMovieYear != null && !TextUtils.isEmpty(year)) {
            mMovieYear.setText(year);
        }
    }

    public void setPreviewRating(float rating) {
        if (mRating != null) {
            mRating.setRating(rating);
        }
    }

    public void setPreviewDate(String date) {
        if (mMovieDate != null && !TextUtils.isEmpty(date)) {
            mMovieDate.setText(date);
        }
    }

    // call from dynamic ui layout inflater. init view status with attributes.
    // add by xxhuang
    @Override
    public void applyAttributeSet(Attributes attrs, int defStyle) {
        mMovieImg = null;
        mMovieTitle = null;
        mMovieYear = null;
        mMovieDate = null;
        mRating = null;
        removeAllViews();
        init(getContext(), attrs.getInt(ATTR_PREVIEW_STYLE, PREVIEW_STYLE_TITLE));
        setPreviewImgByURL(attrs.getString(ATTR_IMAGE_SRC));
        setPreviewTitle(attrs.getString(ATTR_MOVIE_TITLE));
        // setPreviewYear(attrs.getString(ATTR_MOVIE_YEAR));
        // setPreviewDate(attrs.getString(ATTR_MOVIE_DATE));
        // setPreviewRating(attrs.getFloat(ATTR_MOVIE_RATING, 0f));
        // setTag(attrs.get(ATTR_TAG));
        setPreviewYear(attrs.getString(ATTR_IMAGE_TAG));
        setClickable(true);
        setFocusable(true);
        setFocusableInTouchMode(true);
        setBackgroundResource(R.drawable.preview_1_background);

        setTag(attrs);

        final ActionPerformer performer = attrs.get(ActionPerformer.KEY_ACTION, ActionPerformer.class);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    // performer.invoke(v.getContext());
                    
                    Intent intent = new Intent(getContext(), DetailActivity.class);
                    getContext().startActivity(intent);
                    
                    
                } catch (PerformException e) {
                    e.printStackTrace();
                } catch (NullPointerException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    ClickLogSender.getInstance(getContext()).sendLog(v, performer.getArguments().getLogtag());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        // setPadding(0, 0, 0, 0);
    }

    private interface HandleAction {
        int REQUEST_FOCUSE = 10;
    }

    Handler handle = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            case HandleAction.REQUEST_FOCUSE:
                Preview.this.requestFocus();
                break;

            default:
                break;
            }
        }
    };
}
